package com.example.springbootServiceNetwork.demos.web.Controller;

import com.alibaba.fastjson.JSON;
import com.example.springbootServiceNetwork.demos.web.Config.JwtInfo;
import com.example.springbootServiceNetwork.demos.web.Config.Result;
import com.example.springbootServiceNetwork.demos.web.Dto.PageDto;
import com.example.springbootServiceNetwork.demos.web.Entity.User;
import com.example.springbootServiceNetwork.demos.web.Dto.VerificationCode;
import com.example.springbootServiceNetwork.demos.web.Service.EmailCodeService;
import com.example.springbootServiceNetwork.demos.web.Service.JwtRedistService;
import com.example.springbootServiceNetwork.demos.web.Service.QRCodeService;
import com.example.springbootServiceNetwork.demos.web.Service.UserService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static com.example.springbootServiceNetwork.demos.web.Tool.EncryptUtil.encrypt;


@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private EmailCodeService emailCodeService;
    @Resource
    private QRCodeService qrCodeService;
    @Resource
    private JwtRedistService jwtRedistService;

    /**
     * 用户注册
     * cellPhone
     * password
     * userName
     * image
     * email
     */
    @PostMapping("/register")
    public Result register(@RequestBody User user){
     return userService.register(user);
    }

    /**
     * 密码登录
     * cellPhone
     * password
     */
    @PostMapping("/login")
    public Result login(@RequestBody @Validated User user){
        return userService.login(user);
    }

    /**
     * 获取登录二维码
     */
    @GetMapping("/getQRcode")
    public Result getQRcode() throws Exception {
        return qrCodeService.createQRimg();
    }

    /**
     * 扫码登录
     * uuid
     * scInfo
     */
    @PostMapping("/scanCodeLogin")
    public Result scanCodeLogin(@RequestBody Object object){
        return userService.scanCodeLogin(object);
    }

    /**
     * 发送邮箱验证码
     * Email
     */
    @PostMapping("/sendEmail")
    public Result sendEmail(@RequestBody  VerificationCode code){
        return emailCodeService.sendEmail(code);
    }

    /**
     * 验证码登录
     * Email
     * code
     */
    @PostMapping("/emailLogin")
    public Result imailLogin(@RequestBody @Validated VerificationCode code){
        return emailCodeService.verifyEmail(code);
    }

    /**
     * 获取图形验证码
     */
    @GetMapping("/getCode")
    public ServletOutputStream getCode(HttpServletResponse response) throws IOException {
        return emailCodeService.creatCode(response);
    }

    /**
     * 校验图形验证码
     */
    @PostMapping("/verifyCode")
    public Result verifyCode(@RequestBody VerificationCode verificationCode){
        return emailCodeService.verifyCode(verificationCode);
    }

    /**
     * 查询用户信息
     */
    @GetMapping("/selUserInfo")
    public String selUserInfo(@RequestHeader("token") String token){
        JwtInfo jwtInfo = jwtRedistService.getUserInfo(token);
        if(jwtInfo.getPass()){
            return encrypt(JSON.toJSONString(userService.selUserInfo(jwtInfo.getUserId()))) ;
        }else {
            return encrypt(JSON.toJSONString(jwtInfo.getResult())) ;
        }
    }

    /**
     * 校验用户登录
     */
    @GetMapping("/verifyUser")
    public Result verifyUser(@RequestHeader("token") String token){
        JwtInfo jwtInfo = jwtRedistService.getUserInfo(token);
        if(jwtInfo.getPass()){
            return userService.verifyUser(jwtInfo.getUserId());
        }else {
            return  jwtInfo.getResult();
        }
    }

    /**
     * 更新用户信息
     */
    @PostMapping("/updateUser")
    public Result updateUser(@RequestBody User user,@RequestHeader("token") String token){
       JwtInfo jwtInfo = jwtRedistService.getUserInfo(token);
        if(jwtInfo.getPass()){
            return userService.updateUser(user,jwtInfo);
        }else {
            return jwtInfo.getResult();
        }
    }

    /**
     * 更新用户信息
     */
    @PostMapping("/deleteUser")
    public Result deleteUser(@RequestBody User user,@RequestHeader("token") String token){
        JwtInfo jwtInfo = jwtRedistService.getUserInfo(token);
        if(jwtInfo.getPass()){
            return userService.deleteUser(user,jwtInfo);
        }else {
            return jwtInfo.getResult();
        }
    }

    /**
     * 用户离线
     */
    @GetMapping("/updateOffline")
    public Result updateOffline(@RequestHeader("token") String token){
        JwtInfo jwtInfo = jwtRedistService.getUserInfo(token);
        if(jwtInfo.getPass()){
            User user = new User();
            user.setUserId(jwtInfo.getUserId());
            user.setOnlineStatus("0");
            return userService.updateOffline(user);
        }else {
            return jwtInfo.getResult();
        }
    }

    /**
     * 查询所有用户信息（分页查询）
     * 后台专用
     */
    @PostMapping("/queryAllUser")
    public String queryAllUser(@RequestBody PageDto pageDto, @RequestHeader("token") String token){
        JwtInfo jwtInfo = jwtRedistService.getUserInfo(token);
        if(jwtInfo.getPass()){
            return encrypt(JSON.toJSONString(userService.queryAllUser(pageDto,jwtInfo)));
        }else {
            return encrypt(JSON.toJSONString(jwtInfo.getResult())) ;
        }
    }

    /**
     * 更新用户信息(后台专用)
     */
    @PostMapping("/updateAllUser")
    public Result updateAllUser(@RequestBody User user, @RequestHeader("token") String token){
        JwtInfo jwtInfo = jwtRedistService.getUserInfo(token);
        if(jwtInfo.getPass()){
            return userService.updateAllUser(user,jwtInfo);
        }else {
            return jwtInfo.getResult();
        }
    }

    /**
     * 删除用户信息(后台专用)
     */
    @PostMapping("/deleteAllUser")
    public Result deleteAllUser(@RequestBody User user, @RequestHeader("token") String token){
        JwtInfo jwtInfo = jwtRedistService.getUserInfo(token);
        if(jwtInfo.getPass()){
            return userService.deleteAllUser(user,jwtInfo);
        }else {
            return jwtInfo.getResult();
        }
    }


    /**
     * 查询贡献值
     */
    @GetMapping("/queryContributors")
    public Result queryContributors(){
        User user = new User();
        user.setContributors(1);
        return userService.queryContributors(user);
    }


}
